class Access extends DevAccess{
    Employee emp
    Area area
    Container container

    //static belongsTo = [Employee, Area, Container]

    static constraints = {

    }

    public Boolean isBudgetLocked(){
        return container.budgetLocked;
    }


    public List getList(String controller){
        switch(controller){
            case "access" :
            ArrayList list = new ArrayList()

                if(hasAdminAuth()){
                    def areas = container.areas
                    def temp
                    def access
                    for(Area a : areas){
                        temp = Employee.findAllByHiredBy(a)
                        for(Employee e in temp){
                            access = Access.findAllByEmp(e)
                            for(Access ac in access){
                                    if(ac){
                                        list.add(ac)
                                    }
                            }
                        }
                    }
                }
                return list
            break
            
            case "project" :
                if(hasAdminAuth()){
                    ArrayList list = new ArrayList()
                    def areas = container.areas
                    for(Area a : areas){
//                        println("area"+a)
                        def temp
                        if(DBConnection.isConnectionStarted()){
                            temp = DBConnection.findProjectByArea(a)
                        }
                        else{
                            temp = Project.findAllByArea(a)
                        }
                        for( Project p : temp){
                            if(p){
                                list.add(p)
                            }
                        }
                    }
                    return list
                }
                else if(hasManagerAuth()){
                    def temp
                    if(DBConnection.isConnectionStarted()){
                        temp = DBConnection.findProjectByArea(this.area)
                    }
                    else{
                        temp = Project.findAllByArea(this.area)
                    }
                    return temp
                }
                else{
                    List list = new ArrayList()
                    return  list
                }
            break
            case "course" :
                if(hasAdminAuth()){
                    ArrayList  list = new ArrayList()
                    def areas = container.areas
                    for(Area a : areas){
                        def temp = Course.findAllByArea(a)
                        for( Course c : temp){
                            if(c){
                                list.add(c)
                            }
                        }
                    }
                    return list
                }
                else if(hasManagerAuth()){
                    return Course.findAllByArea(this.area)
                }
                else{
                    List list = new ArrayList()
                    return  list
                }
            break
            case "area" :
                if(hasAdminAuth()){
                    return Area.findAllByContainer(this.container)
                }
                else if(hasManagerAuth()){
                    return Area.findAllById(this.area.id)
                }
                else{
                    List list = new ArrayList()
                    return  list
                }
            break
            case "employee" :
                if(hasAdminAuth()){
                    ArrayList emp = new ArrayList()
                    def areas = container.areas
                    for(Area a : areas){
                        def temp
                        temp = DBConnection.getEmployeeListByArea(a)
                        for(Employee e : temp){
                            emp.add(e)
                        }
                    }
                    return emp
                }
                else if(hasManagerAuth()){
                    return DBConnection.getEmployeeListByArea(this.area)
                }
                else{
                    List list = new ArrayList()
                    return  list
                }
            break
            default:
                List list = new ArrayList()
                return  list
            break;
        }
    }

    public Boolean checkAccess(String controller, String action, Long id){
       // if(controller.equalsIgnoreCase("area")){
            switch(action){
                case "show" :
                    def area = Area.get(id)
                    if(hasAdminAuth()){
                        return this.container.equals(Container.get(area.container.id))
                    }
                    if(hasManagerAuth()){
                        def a = Area.get(id)
                        return (this.area.id.equals(a.id) && this.container.id.equals(a.container.id))
                    }
                    return false
                break
                case "create" :
                    if(hasAdminAuth()){
                        return true
                    }
                    else return false
                break
                case "edit" :
                    if(hasAdminAuth()){
                        def area = Area.get(id)
                        return area.container.id == this.container.id
                    }
                    if(hasManagerAuth()){
                        def area = Area.get(id)
                        return (this.area.id.equals(area.id) && this.container.id.equals(area.container.id))
                    }
                    return false
                break
                case "delete" :
                    if(hasAdminAuth()){
                        return this.container.equals(Container.get(id))
                    }
                    return false
                break
                case "save" :
                    if(hasAdminAuth()){
                        return this.container.id == id
                    }
                    return false
                break
                default : return false
                }
            }
           // else{
           //     return false
           // }
        //}
    String toString(){"${this.emp} : ${this.role}"}
    //String toString(){"${this.role}"}
}
